import http from '@/utils/http'
import { delToken, setToken } from '../../utils/token'

/**
 * 用来进行发起登录操作
 * @param data
 * @returns {function(*): Promise<{success: 登录是否成功, message: 消息}>}
 */
export const login = (data) => {
  return async (dispatch) => {
    const res = await http.post('/sys/login', data)
    const { message, data: token, success } = res.data
    if (res.data.success) {
      setToken(token)
      dispatch({
        type: 'login/login',
        payload: res.data.data,
      })
    }
    return { success, message }
  }
}

export const logout = () => {
  return (dispatch) => {
    //1.清除token 本地，redux
    delToken()
    dispatch({
      type: 'login/logout',
    })
    //2.清空个人信息
    dispatch({
      type: 'user/clearUserInfo',
    })
  }
}
